home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / Parser.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.2 KB  |  264 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import sys
  7. from SubstitutionParser import SubstitutionParser
  8.  
  9. class Token:
  10.     
  11.     def __init__(self, klass, data):
  12.         self.klass = klass
  13.         self.data = data
  14.  
  15.     
  16.     def __str__(self):
  17.         return '%s: [%s]' % (self.klass, self.data)
  18.  
  19.     
  20.     def __eq__(self, other):
  21.         if self.klass == other.klass:
  22.             pass
  23.         return self.data == other.data
  24.  
  25.     
  26.     def __ne__(self, other):
  27.         return not self.__eq__(other)
  28.  
  29.  
  30.  
  31. class Parser:
  32.     SREG_ENV = '[A-Z_]+'
  33.     SREG_ID = '[0-9]+'
  34.     REG_ESCAPE = re.compile('(\\$(%s|\\(|\\{|<|%s)|`|\\\\)' % (SREG_ENV, SREG_ID))
  35.     
  36.     def __init__(self, **kwargs):
  37.         for k, v in kwargs.items():
  38.             setattr(self, k, v)
  39.         
  40.         self.position = 0
  41.         self.data_length = len(self.data)
  42.         self.RULES = (self._match_env, self._match_regex, self._match_placeholder, self._match_shell, self._match_eval, self._text)
  43.  
  44.     
  45.     def remains(self):
  46.         return self.data[self.position:]
  47.  
  48.     
  49.     def next_char(self):
  50.         if self.position + 1 >= self.data_length:
  51.             return ''
  52.         return self.data[self.position + 1]
  53.  
  54.     
  55.     def char(self):
  56.         if self.position >= self.data_length:
  57.             return ''
  58.         return self.data[self.position]
  59.  
  60.     
  61.     def token(self):
  62.         self.tktext = ''
  63.         while self.position < self.data_length:
  64.             
  65.             try:
  66.                 func = {
  67.                     '$': self._rule,
  68.                     '`': self._try_match_shell }[self.char()]
  69.             except:
  70.                 func = self._text
  71.  
  72.             if func != self._text and self.tktext != '':
  73.                 return Token('text', self.tktext)
  74.             tk = func()
  75.             if tk:
  76.                 return tk
  77.             continue
  78.             tk
  79.         if self.tktext != '':
  80.             return Token('text', self.tktext)
  81.  
  82.     
  83.     def _need_escape(self):
  84.         text = self.remains()[1:]
  85.         if text == '':
  86.             return False
  87.         return self.REG_ESCAPE.match(text)
  88.  
  89.     
  90.     def _escape(self):
  91.         if not self._need_escape():
  92.             return None
  93.         self.position += 1
  94.  
  95.     
  96.     def _text(self):
  97.         if self.char() == '\\':
  98.             self._escape()
  99.         
  100.         self.tktext += self.char()
  101.         self.position += 1
  102.  
  103.     
  104.     def _rule(self):
  105.         for rule in self.RULES:
  106.             res = rule()
  107.             if res:
  108.                 return res
  109.         
  110.  
  111.     
  112.     def _match_env(self):
  113.         text = self.remains()
  114.         if not re.match('\\$(%s)' % self.SREG_ENV, text):
  115.             pass
  116.         match = re.match('\\${(%s)}' % self.SREG_ENV, text)
  117.         if match:
  118.             self.position += len(match.group(0))
  119.             return Token('environment', match.group(1))
  120.  
  121.     
  122.     def _parse_list(self, lst):
  123.         pos = 0
  124.         length = len(lst)
  125.         items = []
  126.         last = None
  127.         while pos < length:
  128.             char = lst[pos]
  129.             if pos < length - 1:
  130.                 pass
  131.             next = lst[pos + 1]
  132.             if char == '\\':
  133.                 if next == ',' or next == ']':
  134.                     char = next
  135.                     pos += 1
  136.                 elif char == ',':
  137.                     if last != None:
  138.                         items.append(last)
  139.                     
  140.                     last = None
  141.                     pos += 1
  142.                     continue
  143.                 
  144.             if not last != None or last + char:
  145.                 pass
  146.             last = char
  147.             pos += 1
  148.         if last != None:
  149.             items.append(last)
  150.         
  151.         return items
  152.  
  153.     
  154.     def _parse_default(self, default):
  155.         match = re.match('^\\s*(\\\\)?(\\[((\\\\]|[^\\]])+)\\]\\s*)$', default)
  156.         if not match:
  157.             return [
  158.                 default]
  159.         groups = match.groups()
  160.         if groups[0]:
  161.             return [
  162.                 groups[1]]
  163.         return self._parse_list(groups[2])
  164.  
  165.     
  166.     def _match_placeholder(self):
  167.         text = self.remains()
  168.         if not re.match('\\${(%s)(:((\\\\\\}|[^}])+))?}' % self.SREG_ID, text):
  169.             pass
  170.         match = re.match('\\$(%s)' % self.SREG_ID, text)
  171.         if not match:
  172.             return None
  173.         groups = match.groups()
  174.         default = ''
  175.         tabstop = int(groups[0])
  176.         self.position += len(match.group(0))
  177.         return Token('placeholder', {
  178.             'tabstop': tabstop,
  179.             'default': default })
  180.  
  181.     
  182.     def _match_shell(self):
  183.         text = self.remains()
  184.         if not re.match('`((%s):)?((\\\\`|[^`])+?)`' % self.SREG_ID, text):
  185.             pass
  186.         match = re.match('\\$\\(((%s):)?((\\\\\\)|[^\\)])+?)\\)' % self.SREG_ID, text)
  187.         if not match:
  188.             return None
  189.         groups = match.groups()
  190.         if not groups[1] or int(groups[1]):
  191.             pass
  192.         tabstop = -1
  193.         self.position += len(match.group(0))
  194.         return Token('shell', {
  195.             'tabstop': tabstop,
  196.             'contents': contents })
  197.  
  198.     
  199.     def _try_match_shell(self):
  200.         if not self._match_shell():
  201.             pass
  202.         return self._text()
  203.  
  204.     
  205.     def _eval_options(self, options):
  206.         reg = re.compile(self.SREG_ID)
  207.         tabstop = -1
  208.         depend = []
  209.         options = options.split(':')
  210.         for opt in options:
  211.             if reg.match(opt):
  212.                 tabstop = int(opt)
  213.                 continue
  214.             depend += self._parse_list(opt[1:-1])
  215.         
  216.         return (tabstop, depend)
  217.  
  218.     
  219.     def _match_eval(self):
  220.         text = self.remains()
  221.         options = '((%s)|\\[([0-9, ]+)\\])' % self.SREG_ID
  222.         match = re.match('\\$<((%s:)*)((\\\\>|[^>])+?)>' % options, text)
  223.         if not match:
  224.             return None
  225.         groups = match.groups()
  226.         if not groups[0] or self._eval_options(groups[0][:-1]):
  227.             pass
  228.         (tabstop, depend) = (-1, [])
  229.         self.position += len(match.group(0))
  230.         return Token('eval', {
  231.             'tabstop': tabstop,
  232.             'dependencies': depend,
  233.             'contents': groups[5].replace('\\>', '>') })
  234.  
  235.     
  236.     def _match_regex(self):
  237.         text = self.remains()
  238.         content = '((?:\\\\[/]|\\\\}|[^/}])+)'
  239.         match = re.match('\\${(?:(%s):)?\\s*(%s|\\$([A-Z_]+))?[/]%s[/]%s(?:[/]([a-zA-Z]*))?}' % (self.SREG_ID, self.SREG_ID, content, content), text)
  240.         if not match:
  241.             return None
  242.         groups = match.groups()
  243.         if not groups[0] or int(groups[0]):
  244.             pass
  245.         tabstop = -1
  246.         if not groups[2]:
  247.             if not groups[1] or int(groups[1]):
  248.                 pass
  249.         inp = ''
  250.         pattern = re.sub('\\\\([/}])', '\\1', groups[3])
  251.         substitution = re.sub('\\\\([/}])', '\\1', groups[4])
  252.         if not groups[5]:
  253.             pass
  254.         modifiers = ''
  255.         self.position += len(match.group(0))
  256.         return Token('regex', {
  257.             'tabstop': tabstop,
  258.             'input': inp,
  259.             'pattern': pattern,
  260.             'substitution': substitution,
  261.             'modifiers': modifiers })
  262.  
  263.  
  264.